package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserLocationService;
import com.itheima.vo.HouTaiUserVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class HouTaiUserManager {
    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private UserLocationService userLocationService;

    // 分页查询
    public ResponseEntity findUserInfoByPage(Integer pageNum, Integer pageSize,String cid,String keyWord) {
        // 1.调用rpc查询
        PageBeanVo pageBeanVo = userInfoService.houTaiFindByPage(pageNum, pageSize,cid,keyWord);
        //封装Vo
        List<UserInfo> items = (List<UserInfo>) pageBeanVo.getItems();
        ArrayList<HouTaiUserVo> houTaiUserVos = new ArrayList<>();//创建vo
        if (CollectionUtil.isNotEmpty(items)) {
            Long count = 1L;
            for (UserInfo item : items) {
                HouTaiUserVo houTaiUserVo = new HouTaiUserVo();
                houTaiUserVo.setCount(count);count++;//序号
                houTaiUserVo.setId(item.getId());//用户id
                houTaiUserVo.setNickname(item.getNickname());//昵称
                if (item.getGender().equals("man")){
                    houTaiUserVo.setGender("男");//性别
                }else {
                    houTaiUserVo.setGender("女");
                }
                if (item.getUserStatus().equals("1")){
                    houTaiUserVo.setUserState("正常"); //状态
                }else {
                    houTaiUserVo.setUserState("冻结");
                }
                houTaiUserVo.setCity(item.getCity());//城市
                houTaiUserVos.add(houTaiUserVo);//添加vos
            }
        }
        pageBeanVo.setItems(houTaiUserVos);//封装pbv
        // 2.返回
        return ResponseEntity.ok(pageBeanVo);
    }

    //查询用户地址
    public Map<String,String> findUserLocation(String userId) {
        //rpc获取用户坐标
        return userLocationService.findUserLocation(userId);
    }
}
